home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Python 1.3.3 / Python 133 PPC / RunLibScript / RunLibScript.rsrc / PYC_3114___main__ (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  1996-05-20  |  3.7 KB  |  122 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.3)
  3.  
  4. '''Import a module while pretending its name is __main__. This
  5. can be used to run scripts from the PackedLib resource file while pretending
  6. they have been double-clicked.'''
  7. 'Import a module while pretending its name is __main__. This\ncan be used to run scripts from the PackedLib resource file while pretending\nthey have been double-clicked.'
  8. import imp
  9. import sys
  10. import os
  11. import string
  12. import Dlg
  13. import macfs
  14. DIALOG_ID = 512
  15. OK = 1
  16. CANCEL = 2
  17. SCRIPTNAME = 3
  18. ARGV = 4
  19. STDIN_CONS = 5
  20. STDIN_FILE = 6
  21. STDOUT_CONS = 7
  22. STDOUT_FILE = 8
  23. WORKING_DIR = 9
  24. PAUSE = 10
  25.  
  26. def import_as_main(name):
  27.     (suffix, mode, type) = (fp, path)
  28.     if type == imp.PY_SOURCE:
  29.         imp.load_source('__main__', path, fp)
  30.     elif type == imp.PY_COMPILED:
  31.         imp.load_compiled('__main__', path, fp)
  32.     elif type == imp.PY_RESOURCE:
  33.         imp.load_resource('__main__', path)
  34.     
  35.  
  36.  
  37. def interact():
  38.     d = Dlg.GetNewDialog(DIALOG_ID, -1)
  39.     wdir = stdin = stdout = None
  40.     pause = 0
  41.     (tp, in_c_h, rect) = d.GetDialogItem(STDIN_CONS)
  42.     (tp, in_f_h, rect) = d.GetDialogItem(STDIN_FILE)
  43.     (tp, out_c_h, rect) = d.GetDialogItem(STDOUT_CONS)
  44.     (tp, out_f_h, rect) = d.GetDialogItem(STDOUT_FILE)
  45.     (tp, pause_h, rect) = d.GetDialogItem(PAUSE)
  46.     in_c_h = in_c_h.as_Control()
  47.     in_f_h = in_f_h.as_Control()
  48.     out_c_h = out_c_h.as_Control()
  49.     out_f_h = out_f_h.as_Control()
  50.     pause_h = pause_h.as_Control()
  51.     while 1:
  52.         in_c_h.SetControlValue(not stdin)
  53.         in_f_h.SetControlValue(not (not stdin))
  54.         out_c_h.SetControlValue(not stdout)
  55.         out_f_h.SetControlValue(not (not stdout))
  56.         pause_h.SetControlValue(pause)
  57.         n = Dlg.ModalDialog(None)
  58.         if n == OK:
  59.             break
  60.         elif n == CANCEL:
  61.             sys.exit(0)
  62.         elif n == STDIN_CONS:
  63.             stdin = None
  64.         elif n == STDIN_FILE:
  65.             (fss, ok) = macfs.StandardGetFile('TEXT')
  66.             if ok:
  67.                 stdin = fss
  68.             
  69.         elif n == STDOUT_FILE:
  70.             (fss, ok) = macfs.StandardPutFile('stdout:')
  71.             if ok:
  72.                 stdout = fss
  73.             
  74.         elif n == WORKING_DIR:
  75.             (fss, ok) = macfs.GetDirectory()
  76.             if ok:
  77.                 wdir = fss
  78.             
  79.         elif n == PAUSE:
  80.             pause = not pause
  81.         
  82.     (tp, h, rect) = d.GetDialogItem(SCRIPTNAME)
  83.     name = Dlg.GetDialogItemText(h)
  84.     (tp, h, rect) = d.GetDialogItem(ARGV)
  85.     argv = Dlg.GetDialogItemText(h)
  86.     return (name, argv, stdin, stdout, wdir, pause)
  87.  
  88.  
  89. def main():
  90.     curdir = os.getcwd()
  91.     import Res
  92.     
  93.     try:
  94.         Res.OpenResFile('RunLibScript.rsrc')
  95.     except:
  96.         pass
  97.  
  98.     (name, argv, stdin, stdout, wdir, pause) = interact()
  99.     if not name:
  100.         sys.exit(0)
  101.     
  102.     sys.argv = [
  103.         name] + string.split(argv)
  104.     if stdin:
  105.         sys.stdin = open(stdin.as_pathname())
  106.     
  107.     if stdout:
  108.         sys.stdout = open(stdout.as_pathname(), 'w')
  109.     
  110.     if wdir:
  111.         os.chdir(wdir.as_pathname())
  112.     else:
  113.         os.chdir(curdir)
  114.     import_as_main(name)
  115.     if pause:
  116.         sys.exit(1)
  117.     
  118.  
  119. if __name__ == '__main__':
  120.     main()
  121.  
  122.